const app = getApp();
import {
  request
} from "../../utils/request";
import Dialog from '@vant/weapp/dialog/dialog';


Page({
  data: {
    openid: "",
    scene: "",
    cardInfo: {
      isBind: false,
      isSelf: false,
      phone: '',
      vehicleNo: "*******",
      color: "**",
      message: "您好，临时停车，请多包涵。如果需要挪车，请随时联系我，我会尽快赶来。感谢您的理解！",
    },
    showEmergencyPopup: false
  },
  onShow() {
    if (app.globalData.openid) {
      this.setData({
        openid: app.globalData.openid,
      });
      this.getInfoByCode()
    }else {
      const { getOpenid } = require('../../utils/request.js');
      getOpenid().then(openid => {
        this.setData({
          openid: openid,
        });
        this.getInfoByCode()
      });
    }
  },

  onLoad(options) {
    this.setData({
      scene: options.scene
    })
  },
  getInfoByCode(){
    request({
      url: `mini-code/by-identifier/${this.data.scene}/${this.data.openid}`,
      method: "GET",
      redirectUrl : `/pages/move/move?scene=${this.data.scene}`,
      pageCtx: this
    })
      .then(res => {
        if (res.isBind === false) {
          Dialog.confirm({
            message: '该码暂未绑定，是否前往绑定？',
          })
            .then(() => {
              wx.redirectTo({
                url: `/pages/bind/bind?scene=${this.data.scene}`,
              });
            })
            .catch(() => {
              wx.switchTab({
                url: '/pages/index/index',
              });
            });
        }
        if (res.isSelf === true) {
          Dialog.confirm({
            message: '识别到是您的码，要去修改资料吗？',
          })
            .then(() => {
              wx.redirectTo({
                url: `/pages/bind/bind?scene=${this.data.scene}&isSelf=true`,
              });
            })
            .catch(() => {
              wx.switchTab({
                url: '/pages/index/index',
              });
            });
        }

        this.setData({
          cardInfo: {
            isBind: res.isBind,
            isSelf: res.isSelf,
            phone: res.phone,
            vehicleNo: res.vehicleNo,
            color: res.color,
            message: res.message
          }
        });
      })
      .catch(err => console.error("获取小程序码信息失败", err));
  },
  // 联系车主
  makeCall() {
    wx.showModal({
      title: '联系车主',
      content: '系统目前暂未接入虚拟拨号，请注意保护双方隐私',
      confirmText: '立即拨打',
      cancelText: '取消',
      success: (res) => {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: this.data.cardInfo.phone,
            success: () => {
              console.log('拨打电话成功');
            },
            fail: () => {
              wx.showToast({
                title: '拨打失败，请稍后重试',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 显示紧急联系人弹窗
  showEmergencyContact() {
    this.setData({
      showEmergencyPopup: true
    });
  },

  // 关闭弹窗
  onCloseEmergencyPopup() {
    this.setData({
      showEmergencyPopup: false
    });
  },

  // 拨打紧急联系人
  callEmergencyContact() {
    wx.makePhoneCall({
      phoneNumber: '13800138000',
      complete: () => {
        this.setData({
          showEmergencyPopup: false
        });
      }
    });
  }
});
